package com.recycle.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.recycle.system.domain.OrderItem;
import com.recycle.system.domain.dto.NewOrder;
import com.recycle.system.domain.dto.SelectOptions;
import com.recycle.system.domain.vo.ExtendOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.recycle.common.annotation.Log;
import com.recycle.common.core.controller.BaseController;
import com.recycle.common.core.domain.CommonResult;
import com.recycle.common.enums.BusinessType;
import com.recycle.system.domain.Order;
import com.recycle.system.service.IOrderService;
import com.recycle.common.utils.poi.ExcelUtil;
import com.recycle.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 *
 * @author recycle
 * @date 2023-03-11
 */
@RestController
@RequestMapping("/system/order")
@Api(tags = "订单信息")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    @ApiOperation("")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }


    /**
     * 模糊查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/fuzzy")
    @ApiOperation("")
    public TableDataInfo fuzzyList(Order order) throws IllegalAccessException {
        startPage();
        List<Order> list = orderService.fuzzySelectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderId}")
    @ApiOperation("")
    public CommonResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(orderService.selectOrderByOrderId(orderId));
    }

    /**
     * 新增订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("")
    public CommonResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 新增订单(前台)
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("")
    public CommonResult addOrder(@RequestBody NewOrder order)
    {
        return toAjax(orderService.addOrder(order));
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("")
    public CommonResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    @ApiOperation("")
    public CommonResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(orderService.deleteOrderByOrderIds(orderIds));
    }



    /**
     * 查询关联资源信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/resource")
    @ApiOperation("")
    public TableDataInfo resource(Long orderId)
    {
        startPage();
        List<OrderItem> list = orderService.selectRelativeResource(orderId);
        return getDataTable(list);
    }

    /**
     * 查询商家列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/merchant")
    @ApiOperation("")
    public TableDataInfo merchant()
    {
        startPage();
        List<SelectOptions> list = orderService.selectMerchant();
        return getDataTable(list);
    }

    /**
     * 查询历史申请
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/history")
    @ApiOperation("")
    public TableDataInfo historicalHistory(ExtendOrder extendOrder)
    {
        startPage();
        List<ExtendOrder> list = orderService.selectHistory(extendOrder);
        return getDataTable(list);
    }
}
